var e = getApp(), t = e.requirejs("core"), i = e.requirejs("foxui"), a = e.requirejs("jquery");

Page({
    data: {
        member: {},
        binded: !1,
        endtime: 0,
        postData: {},
        submit: !1,
        subtext: "立即登录",
        smsimgcode: "",
        verifycode_img: "",
        wxappopenid:''
    },
    onLoad: function(i) {
        var o=this;

        t.get("wxAppSetting", {}, function(e) {
            var n = e.sysset;
            o.setData({
                shop_logo: n.shoplogo,
                shop_name: n.shopname
            });
        });

        e.url(i),  this.getInfo();
    },
    getInfo: function() {
        var e, i = this;
        var that=this;
        /*获取登录的openid*/
        wx.login({
            success (res) {
                console.log(res);
                if (res.code) {
                    t.post("wxapp/login", {
                        code: res.code
                    }, function(t1) {
                        that.setData({
                            wxappopenid: t1.openid,
                        });

                    })
                }
            }

        })
        t.get("member/login/login2", {}, function(t2) {
            if (t2.member.mobile) {
                /*wx.switchTab({
                    url: "/pages/member/index/index"
                });*/
                 wx.navigateBack({
                    changed: !0
                })
            } else {


                var a = {
                        member: t2.member,
                        binded: t2.binded,
                        endtime: t2.endtime,
                        show: !0,
                        smsimgcode: t2.smsimgcode,
                        verifycode_img: t2.verifycode_img
                    };
                    a.postData = {
                        mobile: t2.member.mobile,
                        wxappopenid: that.data.wxappopenid,
                        code: "",
                        password: "",
                        password1: ""
                    }, i.setData(a), t2.endtime > 0 && i.endTime(), e =  "登录",
                    wx.setNavigationBarTitle({
                        title: e
                    });
            }
        })

    },
    endTime: function() {
        var e = this, t = e.data.endtime;
        if (t > 0) {
            e.setData({
                endtime: t - 1
            });
            setTimeout(function() {
                e.endTime();
            }, 1e3);
        }
    },
    inputChange: function(e) {
        var i = this.data.postData, s = t.pdata(e).type, o = e.detail.value;
        i[s] = a.trim(o), this.setData({
            postData: i
        });
    },
    getCode: function(e) {
        var s = this;
        if (!(s.data.endtime > 0)) {
            var o = s.data.postData.mobile;
            if (a.isMobile(o)) {
                if (1 == s.data.smsimgcode) {
                    var m = s.data.postData.verifyImg;
                    if (null == m) return void i.toast(s, "请填写图形验证码");
                }
                t.get("sms/changemobile", {
                    mobile: o,
                    verifyImgCode: m,
                    smsimgcode: s.data.smsimgcode
                }, function(e) {
                    null == e.error ? (i.toast(s, "短信发送成功"), s.setData({
                        endtime: 60
                    }), s.endTime()) : i.toast(s, e.message);
                }, !0, !0, !0);
            } else i.toast(s, "请填写正确的手机号");
        }
    },
    submit: function(ee) {


        if (!this.data.submit) {
            var s = this, o = this.data.postData;
            o.wxappopenid=this.data.wxappopenid;
            a.isMobile(o.mobile)  ? o.password && "" != o.password ?   (this.setData({
                submit: !0,
                subtext: "正在登录..."
            }), t.post("member/login/submit2", o, function(f) {
                        if (f.error < 0){
                            i.toast(s, f.message)
                            s.setData({
                                submit: !1,
                                subtext: "立即登录",
                                "postData.confirm": 0
                            });

                        }else{
                            i.toast(s, '登录成功',1500,'#BD081B')
                            e.setCache("userinfo", f), e.setCache("userinfo_openid", f.openid),
                            e.setCache("userinfo_id", f.id)

                            e.getSet();
                            /*判断上一层页面是否是register 如果是就返回member */
                            let pages = getCurrentPages();
                            let currPage = null;
                            // console.log(pages) 的到一个数组
                            if (pages.length) {
                              // 获取当前页面的前以页面的对象
                              currPage = pages[pages.length - 2];
                            }

                            // 获取当前页面的前一页面的路由
                            let route = currPage.route
                            console.log(route)
                            if (route=='pages/auth/register/index' || route=='pages/auth/forgetpwd/index'){
                              wx.switchTab({
                                url: "/pages/member/index/index"
                              })
                            }else{
                              wx.navigateBack({

                                delta: 1  // 返回上一级页面。

                              })
                            }

                            /*wx.navigateBack({
                                changed: !0
                            })*/



                            /*wx.switchTab({
                                url: "/pages/member/index/index"
                            }) */

                        }



                    })
            )
            : i.toast(this, "请填写登录密码")  : i.toast(this, "请填写正确的手机号");
        }
    },
    imageChange: function() {
        var e = this;
        t.get("member/bind/imageChange", {}, function(t) {
            e.setData({
                verifycode_img: t.verifycode_img
            });
        });
    }
});